package com.freelance.vn.apps.puzzle.utils;

import java.util.ArrayList;
import java.util.Random;

import com.freelance.vn.apps.puzzle.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

public class Background {
	private int width = 0; 
	private int height = 0;
	private int xoffsetLandscape = 0;
	private int xoffsetportrait = 5;
	private int ROWS = 6; 
	private int COLUMNS = 4; 
	private Context mContext;
	private PlayAreaView mParent;
	private Paint mPaint;
	private Pictures[] pictures;
	private Display display;
	private int reqPlaywidth = 0;
	
	public int cellSizeWidth = 0;
	public int cellSizeHeight = 0;
	
	Rect destination = new Rect();
	public class picturesCellPosition {
		public int x;
		public int y;
	}
	public ArrayList<picturesCellPosition> picturesPos = new ArrayList<picturesCellPosition>();
	
	public ArrayList <Integer> randomNos ;
	private Random randomizer;
	private boolean isPortrait;
	private boolean isUpdatePictureCellsPositions;
	
	public Background( Context context, Paint paint, PlayAreaView parent, Pictures[] aPic ) {
		mContext = context;
		mPaint = paint;
		mParent = parent;
		pictures = aPic;
		randomizer = new Random();
		randomNos = new ArrayList<Integer>();
		
		display = ((WindowManager)mContext.getSystemService(mContext.WINDOW_SERVICE)).getDefaultDisplay();  
		  
		width = display.getWidth();  
		height = display.getHeight(); 
//		width=parent.getWidth();
//		height=parent.getHeight();
		
		if ( width == 320 || width == 240 ) {
			mPaint.setTextSize(15);	
		} else {
			mPaint.setTextSize(25);
		}
		
		updateBackground();
	}
	
	 public void updateBackground() {
		reqPlaywidth = display.getWidth();
		
		if ( width > height ) {
			reqPlaywidth = display.getHeight();
			isPortrait = false;
		} else {
			isPortrait = true;
		}
		mParent.invalidate();
		isUpdatePictureCellsPositions = true;
	}
	 
	public Bitmap getBackgroundImage( boolean isRandom, int _width, int _height ) {
		this.width = _width;
		this.height = _height;
		Bitmap bitmap;
		Bitmap mbitmap;
		bitmap = Bitmap.createBitmap( width, height, Config.ARGB_8888 );
		Canvas drawcanvas = new Canvas( bitmap );
		
		/*if(isPortrait)
			mbitmap = BitmapFactory.decodeResource(mContext.getResources(), com.freelance.vn.apps.puzzle.R.drawable.background);
		else
			mbitmap = BitmapFactory.decodeResource(mContext.getResources(), com.freelance.vn.apps.puzzle.R.drawable.landscape);*/
		
        //drawcanvas.drawBitmap( mbitmap, 0, 0, mPaint );
        
        mbitmap = BitmapFactory.decodeResource(mContext.getResources(), com.freelance.vn.apps.puzzle.R.drawable.empty_photo);
        
		int yOffset;
		int xOffset;
				 
		if ( isPortrait ) {
			cellSizeWidth = ( width - ( xoffsetportrait * 2 ) ) / COLUMNS;
			cellSizeHeight = ( cellSizeWidth * 170 ) / 192;
			yOffset = height - ( cellSizeHeight * ROWS );
			xOffset = xoffsetportrait;
		} else {
			cellSizeHeight = height / ROWS;
			cellSizeWidth = ( cellSizeHeight * 192 ) / 170;	
			xOffset = ( width - ( cellSizeWidth * COLUMNS ) ) / 2;
			yOffset = xoffsetLandscape;
		}
		
		int rightOffset = xOffset + cellSizeWidth;
		int bottomOffset = yOffset + cellSizeHeight;
			
		for ( int i = 0; i < ROWS; i++ ) {
			for ( int j = 0; j < COLUMNS; j++ ) {
				destination.top = yOffset;
				destination.left = xOffset;
				destination.right = rightOffset;
				destination.bottom = bottomOffset;
	
				if ( isUpdatePictureCellsPositions ) {
					picturesCellPosition obj = new picturesCellPosition();
					obj.x = destination.left;
					obj.y = destination.top;
					picturesPos.add(obj);
				}
				
				drawcanvas.drawBitmap( mbitmap, null, destination, mPaint);
				xOffset = xOffset + cellSizeWidth;
				rightOffset = xOffset + cellSizeWidth;
			} // End of inner for loop

			if ( isPortrait ) {
				xOffset = xoffsetportrait;
			} else {
				xOffset = xoffsetLandscape;
			}
			 		
			rightOffset = xOffset + cellSizeWidth;
			yOffset = yOffset + cellSizeHeight;
			bottomOffset = bottomOffset + cellSizeHeight;
		}
		isUpdatePictureCellsPositions = false;
		
		if ( isPortrait ) {
			yOffset = height - ( cellSizeHeight * ROWS );
			xOffset = xoffsetportrait;
		} else {
			xOffset = ( width - ( cellSizeWidth * COLUMNS ) ) / 2;
			yOffset = xoffsetLandscape;
		}
				
		rightOffset = xOffset + cellSizeWidth;
		bottomOffset = yOffset + cellSizeHeight;
		int numbersCount = 0;
		int index;

		for ( int i = 0; i < ROWS; i++ ) {
			for ( int j = 0; j < COLUMNS; j++ ) {
				if ( numbersCount < 24 ) {
					index = randomNos.get( numbersCount );
					if ( isRandom || pictures[index].getX() == 0 ) {
						destination.top = yOffset;
						destination.left = xOffset;
						destination.right = rightOffset;
						destination.bottom = bottomOffset;
						pictures[index].setX( xOffset );
						pictures[index].setY( yOffset );
						pictures[index].setRight( rightOffset );
						pictures[index].setBottom( bottomOffset );
					} else {
						destination.top = pictures[index].getY();
						destination.left = pictures[index].getX();
						destination.right = pictures[index].getRight();
						destination.bottom = pictures[index].getBottom();
					}
					
					if ( index != 23 ) {
						drawcanvas.drawBitmap( pictures[index].getBitmap(), null, destination, mPaint );
					}
					numbersCount++;
				}
				xOffset = xOffset + cellSizeWidth;
				rightOffset = xOffset + cellSizeWidth;
			}

			if ( isPortrait ) {
				xOffset = xoffsetportrait;
			} else {
				xOffset = ( width - ( cellSizeWidth * COLUMNS ) ) / 2;
			}

		    rightOffset = xOffset + cellSizeWidth;
			yOffset = yOffset + cellSizeHeight;
			bottomOffset = bottomOffset + cellSizeHeight;
		}
				
		return bitmap;
	}
	
	public void generateRandomNos() {
        int number = Math.abs( randomizer.nextInt( 23 ) );
        if( randomNos.size() > 0 )
        	randomNos.clear();

		for ( int i = 0; i < 24; i++ ) {
			while ( isalreadyAdded( number ) == false ) {
				number = Math.abs( randomizer.nextInt( 24 ) );
			}
			randomNos.add( number );
		}
	}
	
	public boolean isalreadyAdded( int number ) {
		boolean numberFound = true;
        
		if(randomNos.size() == 0) {
			return numberFound;
        } else {
			for ( int i = 0; i < randomNos.size(); i++ ) {
				if ( randomNos.get( i ) == number ) {
					numberFound = false;
					break;
				}
			}	 
        }
		return numberFound;
	}
	
	public boolean isGameFinished() {
		boolean isGameFinish = true;
		for ( int i = 0 ; i< pictures.length; i++ ) {
			if(pictures[i].getX() == picturesPos.get(i).x && pictures[i].getY() == picturesPos.get(i).y) {
				//System.out.println("Pictures dont match");
				//return isGameFinish;
			} else {
				isGameFinish = false;
				break;
			}
		}
		return isGameFinish;
	}
}
